package com;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Sai
 * Created by Sai on 2019-03-15.
 *
 *
 * @JsonbProperty - 用于指定自定义字段名称
 * @JsonbTransient - 当我们想要在反序列化/序列化期间忽略该字段时
 * @JsonbDateFormat - 当我们想要定义日期的显示格式时
 * @JsonbNumberFormat - 用于指定数值的显示格式
 * @JsonbNillable - 用于启用空值的序列化
 */

public class RegisterDTO {
    private String userid;
    private String email;
    private String password;
    private String confirm;

    private Map<String,String> errors=null;

    public RegisterDTO() {
        this.userid="";
        this.email="";
        this.password="";
        this.confirm="";
        this.errors=new HashMap<String, String>();
    }

    public boolean isVaildate(){
        boolean flag=true;
        if(!this.userid.matches("\\w{6,15}")){//用户名长度为6-15
            flag=false;
            this.userid="";
            this.errors.put("errname","用户名需要为6-15位字母或数字");
        }
        if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")){
            flag=false;
            this.email="";
            this.errors.put("erremail","输入email地址错误");
        }
        if(!this.password.matches("\\w{6,18}")){
            flag=false;
            this.password="";
            this.errors.put("errpwd","密码需要6-18位字母或数字");
        }
        if(!this.confirm.equals(this.password)){
            flag=false;
            this.confirm="";
            this.errors.put("errconfirm","密码与确认密码不符");
        }
        if(flag){
            //将注册信息写入文本文件中
            try {
                OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream("Userpwd.txt"),"UTF-8");
                OutputStreamWriter pinfowriter=new OutputStreamWriter(new FileOutputStream("User.txt"),"UTF-8");
                writer.append(userid+",");
                writer.append(email+",");
                writer.append(password+"\n");
                writer.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    public String getErrMsg(String key){
        String value=this.errors.get(key);
        //若无错误信息，返回空值
        return value==null?"":value;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }

}